<?php
/**
 * Created by PhpStorm.
 * User: jack Wang
 * Date: 2021/10/29
 * Time: 11:48
 */

class Activity extends Base_Controller
{
    private $conn;

    public function __construct()
    {
        parent::__construct();
        $this->load->service('Activity_service');
        $this->load->model('Activity_model');
        $this->conn = getRedis();
        $this->conn->selectDb(3);
    }

    //掷骰子操作
    public function rollDice()
    {
        $data = $this->input->post();
        $uid = empty($data['uid']) ? 0 : $data['uid'];
        $this->Activity_service->rollDice($uid);
    }

    //获取用户步数排行榜
    public function stepRank()
    {
        $data = $this->input->post();
        $uid = empty($data['uid']) ? 0 : $data['uid'];
        $this->Activity_service->stepRank($uid);
    }

    //掷骰子活动首页
    public function diceIndex()
    {
        $data = $this->input->post();
        $uid = empty($data['uid']) ? 0 : $data['uid'];
        $this->Activity_service->diceIndex($uid);
    }

    //获取盲盒操作
    public function getBlindBox()
    {
        $data = $this->input->post();
        if (empty($data['uid']) || empty($data['num'])) {
            fail(400, '缺少参数!');
        }
        $this->Activity_service->getBlindBox($data['uid'], $data['num']);
    }

    //开启盲盒操作
    public function openBlindBox()
    {
        $data = $this->input->post();
        if (empty($data['uid']) || empty($data['num'])) {
            fail(400, '参数错误!');
        }
        success($this->Activity_service->openBlindBox($data['uid'], $data['num']));
    }

    // 字符兑换奖励操作
    public function charExchange()
    {
        $data = $this->input->post();
        if (empty($data['uid']) || $data['index'] < 0) {
            fail(400, '参数错误!');
        }
        $this->Activity_service->charExchange($data['uid'], $data['index']);
    }

    //用户字符已兑换奖励列表
    public function charExchangeAward()
    {
        $data = $this->input->post();
        if (empty($data['uid'])) {
            fail(200, '参数错误!');
        }
        $this->Activity_service->charExchangeAward($data['uid']);
    }

    //用户字符兑换奖励页面
    public function charExchangeAwardIndex()
    {
        $data = $this->input->post();
        if (empty($data['uid'])) {
            fail(400, '参数错误!');
        }
        $this->Activity_service->charExchangeAwardIndex($data['uid']);
    }

    //盲盒交友
    //放入纸条到盲盒
    public function sendBlindBoxNote()
    {
        $data = $this->input->post();
        $rules = [
            [
                'field' => 'uid',
                'label' => '用户id',
                'rules' => 'required',
                'errors' => [
                    'required' => '用户id不能为空'
                ],
            ],
            [
                'field' => 'code',
                'label' => '微信号',
                'rules' => 'required|max_length[100]',
                'errors' => [
                    'required' => '微信号不能为空',
                    'max_length' => '微信号长度错误',
                ],
            ],
            [
                'field' => 'type',
                'label' => '用户性别',
                'rules' => 'required|in_list[1,2]',
                'errors' => [
                    'required' => '性别不能为空',
                    'in_list' => '性别只能为男、女',
                ],
            ],
            [
                'field' => 'content',
                'label' => '放入盲盒内容',
                'rules' => 'required',
                'errors' => [
                    'required' => '内容不能为空',
                ],
            ],
        ];
        $checkParams = $this->validate($data, $rules);
        //参数验证
        if ($checkParams !== true) {
            fail(400, $checkParams);
        }
        if ($data['code'] && !preg_match('#^\w+$#', $data['code'])) {
            fail(400, '微信号仅限数字和字符');
        }
        $this->Activity_service->sendBlindBoxNote($data);
    }

    //抽取纸条
    public function getBlindBoxNote()
    {
        $data = $this->input->post();
        $rules = [
            [
                'field' => 'uid',
                'label' => '用户id',
                'rules' => 'required',
                'errors' => [
                    'required' => '用户id不能为空'
                ],
            ],
            [
                'field' => 'type',
                'label' => '盲盒类型',
                'rules' => 'required|in_list[1,2]',
                'errors' => [
                    'required' => '盲盒类型不能为空',
                    'in_list' => '盲盒类型错误'
                ],
            ],
        ];
        $checkParams = $this->validate($data,$rules);
        if ($checkParams !== true){
            fail(400,$checkParams);
        }
        $this->Activity_service->getBlindBoxNote($data);
    }

    //我的纸条
    public function myBlindBoxNote()
    {
        $data = $this->input->post();
        $rules = [
            [
                'field' => 'uid',
                'label' => '用户id',
                'rules' => 'required',
                'errors' => [
                    'required' => '用户id不能为空'
                ],
            ],
            [
                'field' => 'type',
                'label' => '类型',
                'rules' => 'required|in_list[1,2]',
                'errors' => [
                    'required' => '类型不能为空',
                    'in_list' => '类型只能为抽取、或放入',
                ],
            ],
        ];
        $checkParams = $this->validate($data, $rules);
        $this->Activity_service->myBlindBoxNote($data['uid'], $data['type']);
    }

    //开启魔盒
    public function openMagicBox()
    {
        $data = $this->input->post();
        if (empty($data['uid'])) {
            fail(400, '参数错误!');
        }
        $num = empty($data['num']) ? 0 : $data['num'];
        $res = $this->Activity_service->openMagicBox($data['uid'], $num);
        responseArray($res);
    }

    //获得魔盒
    public function getMagicBox()
    {
        $data = $this->input->post();
        if (empty($data['uid'])) {
            fail(400, '参数错误!');
        }
        responseArray($this->Activity_service->getMagicBox($data['uid'],2));
    }

    //抽取拼图碎片
    public function getPuzzlePieces()
    {
        $data = $this->input->post();
        $rules = [
            [
                'field' => 'uid',
                'label' => '用户id',
                'rules' => 'required|numeric|greater_than[0]',
            ]
        ];
        $res = $this->validate($data, $rules);
        if ($res != true) {
            fail(400, $res);
        }
        $res = $this->Activity_service->getPuzzlePieces($data['uid']);
        responseArray($res);
    }

    //获取集齐拼图奖励
    public function getPuzzleAward()
    {
        $data = $this->input->post();
        $rules = [
            [
                'field' => "uid",
                'label' => '用户id',
                'rules' => "required|numeric|greater_than[0]"
            ],
            [
                'field' => "type",
                "label" => "领取奖励类型",
                "rules" => "required|in_list[a,b,c,d,e]"
            ],
            [
                'field' => "is_right",
                "label" => "是否右边奖励",
                "rules" => "required|in_list[0,1]"
            ],
        ];
        $status = $this->validate($data,$rules);
        if ($status !== true){
            fail(400,$status);
        }
        $res = $this->Activity_service->getPuzzleAward($data['uid'],$data['type'],$data['is_right']);
        responseArray($res);
    }

    //拼图活动首页
    public function PuzzleIndex()
    {
        $data = $this->input->post();
        if (empty($data['uid'])) {
            fail(400,'参数错误');
        }
        $res = $this->Activity_service->PuzzleIndex($data['uid']);
        responseArray($res);
    }
    public function test()
    {
        $this->Activity_model->useCoinGetTicket();
    }

    //新春礼盒首页
    public function NewYearGiftCaseIndex()
    {
        $data = $this->input->post();
        if (empty($data['uid'])) {
            fail(400, '参数错误');
        }
        responseArray($this->Activity_service->NewYearGiftCaseIndex($data['uid']));
    }

    //开启礼盒
    public function openNewYearCase()
    {
        $data = $this->input->post();
        $rules = [
            [
                'field' => 'uid',
                'label' => '用户id',
                'rules' => 'required|numeric|greater_than_equal_to[0]',
            ],
            [
                'field' => 'index',
                'label' => '礼盒编号',
                'rules' => 'required|numeric|greater_than_equal_to[0]|less_than_equal_to[12]',
            ],
        ];
        $status = $this->validate($data,$rules);
        if ($status !== true){
            fail(400,$status);
        }
        responseArray($this->Activity_service->openNewYearCase($data['uid'],$data['index']));
    }

    //使用新春礼券兑换道具
    public function exchangePropByNYTicket()
    {
        $data = $this->input->post();
        if (empty($data['uid'])) {
            fail(400, '用户id不能为空');
        }
        if (empty($data['index'])) {
            fail(400, '参数错误');
        }
        responseArray($this->Activity_service->exchangePropByNYTicket($data['uid'], $data['index']));
    }

    //新春礼券兑换奖励首页
    public function exchangePropByNYTicketIndex()
    {

    }
}